<?php

namespace Home\Model\Ajax;

class Favorites extends AbstractAjax
{
    // 问题加收藏
    public function handle()
    {
        $qid = I('request.qid', '', 'intval');
        $aid = I('request.aid', '', 'intval');
        $bid = I('request.bid', '', 'intval');
        $type = I('request.type', '', 'intval');
        if ($type == 1) {
            $objid = $qid;
        } elseif ($type == 2) {
            $objid = $aid;
        } elseif ($type == 3) {
            $objid = $bid;
        } else {
            $ajax['status'] = 0;
            $ajax['content'] = "类型错误！";
            return $ajax;
        }
        if (empty($this->uid)) {
            $ajax['status'] = 0;
            $ajax['content'] = "请登录后操作！";
            return $ajax;
        }
        if (empty($type)) {
            $ajax['status'] = 0;
            $ajax['content'] = "数据错误！";
            return $ajax;
        }
        $chenk = D('UserFavorites')->chenk_favorites($this->uid, $objid, $type);
        if ($chenk) {
            D('UserFavorites')->clear_favorites($this->uid, $objid, $type);

            $ajax['status'] = 1;
            $ajax['count'] = D('UserFavorites')->get_favorites_count($objid, $type);
            $ajax['content'] = '取消收藏成功！';
            return $ajax;
        } else {
            D('UserFavorites')->add_favorites($this->uid, $objid, $type);

            $ajax['status'] = 1;
            $ajax['count'] = D('UserFavorites')->get_favorites_count($objid, $type);
            $ajax['content'] = '加入收藏成功！';
            return $ajax;
        }
    }

}